package com.hubu.server;
import com.alibaba.fastjson.JSON;
import com.hubu.config.DatabaseConfig;
import com.hubu.config.DatabaseConfigBuilder;
import com.hubu.core.Database;
import com.hubu.core.DatabaseManager;
import com.hubu.core.Process;
import com.hubu.core.Result;
import com.hubu.json.Command;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Server {
    private DatabaseManager databaseManager;
    private Process process;
    public Server(int port,Process process){
       this.process=process;
    }
    private int port;
    private ServerSocket serverSocket;
    private DatabaseConfig databaseConfig;
    public Server(int port){
        this.port=port;
        this.process=new Process();
    }
    public Server(DatabaseConfig databaseConfig){
        this.databaseConfig=databaseConfig;
    }

    public void start(){
        try {
            serverSocket=new ServerSocket();
            serverSocket.bind(new InetSocketAddress(port));

            //接收连接
            accept();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void accept() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                System.out.println("接收客户端的请求");
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();
                int read = 0;
                byte[] buffer = new byte[1024];
                StringBuffer stringBuffer = new StringBuffer();
                while ((read = inputStream.read(buffer)) != -1) {
                    stringBuffer.append(new String(buffer, 0, read));
                }

                Command command;
                command = JSON.parseObject(stringBuffer.toString(), Command.class);
                Database database = databaseManager.activeDatabase(0);
                Result result = process(databaseManager.activeDatabase(0), this.process, command);

                processWrite(result, socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void processWrite(Result result, OutputStream outputStream){
        String s = JSON.toJSONString(result);
        try {
            outputStream.write(s.getBytes(StandardCharsets.UTF_8));

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private Result process(Database database,
                           Process process,
                           Command command){
        Result result = this.process.process(database,command);
        return result;
}
    private void initServer(DatabaseConfig databaseConfig){
        if(databaseConfig==null){
            databaseConfig= DatabaseConfigBuilder.buildDatabaseConfig();
        }
        this.port=databaseConfig.getServerPort();
    }
    private void initDatabaseManager(DatabaseConfig databaseConfig){
        if(databaseConfig==null){
            databaseConfig= DatabaseConfigBuilder.buildDatabaseConfig();
        }
        databaseManager=DatabaseManager.getInstance(databaseConfig.getDbCount());
    }
    public static void main(String[] args) {
        Server server=new Server(9090);
        server.start();
    }
}
